﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebTraderLiteCL;

namespace WebTraderLite.Account
{
    public partial class Register : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            RegisterUser.ContinueDestinationPageUrl = Request.QueryString["ReturnUrl"];
        }

        protected void RegisterUser_CreatedUser(object sender, EventArgs e)
        {

            User usr = new User();
            usr.setFirstName(((TextBox)this.RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("tbFirstName")).Text);
            usr.setLastName(((TextBox)this.RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("tbLastName")).Text);
            usr.setEmail(this.RegisterUser.Email);
            usr.setUserAddress(((TextBox)this.RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("tbAddress")).Text);
            usr.setzipCode(((TextBox)this.RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("tbZipCode")).Text);
            usr.setUserID(this.RegisterUser.UserName);
            usr.setPassword(this.RegisterUser.Password);
            usr.setAccBalance((float)0.0);
            usr.setAccType("GENERAL");
            usr.setAccStatus("active".ToUpper());
            DBTransactions trans = new DBTransactions();
            trans.OpenDB();
            int d= trans.AddNewUser(usr);

            trans.CloseDB();

            Roles.AddUserToRole(RegisterUser.UserName, "User"); //add new users to User role by default
            FormsAuthentication.SetAuthCookie(RegisterUser.UserName, false /* createPersistentCookie */);
            
            string continueUrl = RegisterUser.ContinueDestinationPageUrl;

            if (String.IsNullOrEmpty(continueUrl))
            {
                continueUrl = "~/";
            }
            Response.Redirect(continueUrl);
        }
        
        
    }
}
